package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 林道样
 * @date 2020-02-20 9:49
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    CheckItemDao checkItemDao;

    @Override
    public void addCheckItem(CheckItem checkItem) {
        checkItemDao.addCheckItem(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> checkItems = checkItemDao.findPage(queryString);
        long total = checkItems.getTotal();
        return new PageResult(total,checkItems);
    }
    @Override
    public Page<CheckItem> findAll() {
        Page<CheckItem> checkItems = checkItemDao.findAll();
        return checkItems;
    }


    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem = checkItemDao.findById(id);
        return checkItem;
    }

    @Override
    public void delete(Integer id) {
        int item = checkItemDao.findItemAndGroup(id);
        if(item<=0){
            checkItemDao.delete(id);
        }else {
            new RuntimeException();
        }
    }



    @Override
    public void update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }
}
